package org.dreamwork.plugins.network.assistant.gui

import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.uiDesigner.core.GridConstraints
import com.intellij.uiDesigner.core.GridLayoutManager
import com.intellij.uiDesigner.core.Spacer
import org.dreamwork.plugins.network.assistant.gui.Utils.toHex
import org.dreamwork.util.StringUtil
import java.awt.Dimension
import java.awt.Insets
import java.awt.event.*
import javax.swing.*

/**
 * Created by seth.yang on 2018/5/7
 */
class SetupForm (title: String?) : DialogWrapper (true), ActionListener {
    interface OnSelectListener {
        fun accept(config: Config?)
    }

    private lateinit var txtHead: JTextField
    private lateinit var txtTail: JTextField
    private lateinit var cbHead: JCheckBox
    private lateinit var cbTail: JCheckBox
    private lateinit var cbCheck: JCheckBox
    private lateinit var cbType: JComboBox<CRCType?>
    private lateinit var cbIncludeHead: JCheckBox
    private lateinit var btnOK: JButton
    private lateinit var btnCancel: JButton
    private lateinit var root: JPanel
    private var listener: OnSelectListener? = null

    init {
        initialComponents()
        init ()
        setTitle (title)
        guiSetup ()
    }

    private fun initialComponents() {
        root = JPanel()
        root.layout = GridLayoutManager(6, 4, Insets(0, 0, 0, 0), -1, -1)
        root.border = BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5), null)
        cbHead = JCheckBox()
        cbHead.text = "固定头"
        root.add(cbHead, GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK or GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        val spacer1 = Spacer()
        root.add(spacer1, GridConstraints(5, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false))
        txtHead = JTextField()
        root.add(txtHead, GridConstraints(0, 1, 1, 3, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, Dimension(150, -1), null, 0, false))
        cbTail = JCheckBox()
        cbTail.text = "固定尾"
        root.add(cbTail, GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK or GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        txtTail = JTextField()
        root.add(txtTail, GridConstraints(1, 1, 1, 3, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, Dimension(150, -1), null, 0, false))
        cbCheck = JCheckBox()
        cbCheck.text = "校验位"
        root.add(cbCheck, GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK or GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        cbType = ComboBox<CRCType?>()
        cbType.model = DefaultComboBoxModel<CRCType?>()
        root.add(cbType, GridConstraints(2, 1, 1, 3, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        cbIncludeHead = JCheckBox()
        cbIncludeHead.text = "包含头"
        root.add(cbIncludeHead, GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK or GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        val spacer2 = Spacer()
        root.add(spacer2, GridConstraints(5, 1, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false))
        btnOK = JButton()
        btnOK.text = "确定"
        root.add(btnOK, GridConstraints(4, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK or GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        btnCancel = JButton()
        btnCancel.text = "取消"
        root.add(btnCancel, GridConstraints(4, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK or GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
    }

    private fun guiSetup() {
        btnOK.addActionListener(this)
        btnCancel.addActionListener(this)
        for (type in CRCType.values()) {
            cbType.addItem(type)
        }

/*
        addMouseListener (object : MouseAdapter() {
            override fun mouseClicked(e: MouseEvent) {
                println (e.button)
            }
        } as MouseListener)
*/
/*
        contentPane = root
        pack()
        val parent: Component = parent
        val D = parent.size
        val d = size
        val x = (D.width - d.width) / 2
        val y = (D.height - d.height) / 2
        setLocation(x, y)
        val url = javaClass.classLoader.getResource("network.png")
        val icon = ImageIcon(url, null)
        setIconImage(icon.image)
*/
    }

    override fun createActions(): Array<Action> {
        return arrayOf ()
    }

    fun setOnSelectListener(listener: OnSelectListener?) {
        this.listener = listener
    }

    override fun actionPerformed(e: ActionEvent) {
        if (e.source === btnOK) {
            if (listener != null) {
                val config = Config()
                config.type = cbType.selectedItem as CRCType
                if (cbHead.isSelected) {
                    val text = txtHead.text
                    if (!StringUtil.isEmpty(text)) {
                        config.head = toHex(text)
                    }
                }
                if (cbTail.isSelected) {
                    val text = txtTail.text
                    if (!StringUtil.isEmpty(text)) {
                        config.tail = toHex(text)
                    }
                }
                config.includeHead = cbIncludeHead.isSelected
                listener!!.accept(config)
            }
            dispose()
        } else if (e.source === btnCancel) {
            if (listener != null) {
                listener!!.accept(null)
            }
            dispose()
        }
    }

    override fun createCenterPanel(): JComponent {
        return root
    }
}